subroutine freemem
  use global_param,only:n_point_type
  use global_param,only:pdefect
  use global_param,only:n_ext
  use global_param,only:extern_type
  use group_param,only:n_object_group
  use group_param,only:object_group
  use group_param,only:n_react_group
  use group_param,only:react_group
  use motion_param,only:n_motion
  use motion_param,only:mtype
  use run_param,only:n_temp_profile
  use run_param,only:temp_profile
  use label_data_param,only:n_obj_type
  use label_data_param,only:obj_type
  use react_data_param,only:n_react_type
  use react_data_param,only:react_type
  use react_data_param,only:react_map
  use geom_data_param,only:n_geom_obj
  use geom_data_param,only:geom_obj
  use sink_data_param,only:n_sink_type
  use sink_data_param,only:sink_type
  use sink_data_param,only:sink_map
  use box_param,only:vbox_map
  use box_param,only:vbox
  use profile_map_param,only:profile_map_diff
  use profile_map_param,only:profile_map_diss
  use profile_map_param,only:profile_map_rctn
  use profile_map_param,only:profile_map_extn
  use profile_map_param,only:profile_map_sink
  use profile_map_param,only:profile_map_srce
  use table_param
  use count_param,only:count_obj
  use count_param,only:total_count_obj
  use output_param,only:print_spatial_dist
  use count_param,only:spatial_file_name
  implicit none

  ! Dummies
  integer :: i,j

  ! Point defect
  do i=1,n_point_type
     deallocate(pdefect(i)%lowercase)
     deallocate(pdefect(i)%uppercase)
  end do
  deallocate(pdefect)

  ! External event
  do i=1,n_ext
     if (extern_type(i)%event_type.eq.1) then
        if (extern_type(i)%n_objset.gt.0) then
           do j=1,extern_type(i)%n_objset
              if (extern_type(i)%objset(j)%n_obj.gt.0) &
                   deallocate(extern_type(i)%objset(j)%obj)
           end do
           deallocate(extern_type(i)%objset)
        end if
        if (extern_type(i)%n_cas.gt.0) then
           do j=1,extern_type(i)%n_cas
              if (extern_type(i)%cascade_list(j)%nlist.gt.0) &
                   deallocate(extern_type(i)%cascade_list(j)%clist)
           end do
           deallocate(extern_type(i)%cgrid)
           deallocate(extern_type(i)%cprob)
           deallocate(extern_type(i)%cascade_list)
           deallocate(extern_type(i)%count_cascade)
        end if
        if (extern_type(i)%n_dpa.gt.0) then
           deallocate(extern_type(i)%dgrid)
           deallocate(extern_type(i)%dprob)
           deallocate(extern_type(i)%count_dpa)
        end if
     else if (extern_type(i)%event_type.eq.3) then
        deallocate(extern_type(i)%source_name)
        deallocate(extern_type(i)%source_type)
        if (extern_type(i)%n_dpa.gt.0) then
           deallocate(extern_type(i)%dgrid)
           deallocate(extern_type(i)%dprob)
           deallocate(extern_type(i)%count_dpa)
        end if
     end if
  end do
  if (n_ext.gt.0) deallocate(extern_type)

  ! Group type
  do i=1,n_object_group
     deallocate(object_group(i)%point_type)
     deallocate(object_group(i)%min_size)
     deallocate(object_group(i)%max_size)
  end do
  if (n_object_group.gt.0) deallocate(object_group)
  
  ! React group
  if (n_react_group.gt.0) deallocate(react_group)

  ! Motion types
  if (n_motion.gt.0) deallocate(mtype)

  ! Temperature profile
  if (n_temp_profile.gt.0) deallocate(temp_profile)

  ! object_type
  do i=1,n_obj_type
     deallocate(obj_type(i)%config)
     if (obj_type(i)%n_group_belong.gt.0) &
          deallocate(obj_type(i)%group_list)
     ! diffusion data : not a case
     ! dissociation data
     if (obj_type(i)%does_diss) then
        do j=1,obj_type(i)%diss_type
           deallocate(obj_type(i)%diss_obj(j)%diss_type)
        end do
        deallocate(obj_type(i)%diss_eb)
        deallocate(obj_type(i)%diss_nu)
        deallocate(obj_type(i)%diss_rd)
        deallocate(obj_type(i)%diss_const_rate)
        deallocate(obj_type(i)%n_diss)
        deallocate(obj_type(i)%diss_obj)
     end if
     ! reaction_group
     if (obj_type(i)%n_diss_react_group.gt.0) &
          deallocate(obj_type(i)%diss_react_group)
     if (obj_type(i)%n_free_react_group.gt.0) &
          deallocate(obj_type(i)%free_react_group)
  end do
  if (n_obj_type.gt.0) deallocate(obj_type)

  ! react_type
  do i=1,n_react_type
     deallocate(react_type(i)%react_eb)
     deallocate(react_type(i)%react_nu)
     deallocate(react_type(i)%react_rd)
     deallocate(react_type(i)%react_rd2)
     deallocate(react_type(i)%react_const_rate)
     do j=1,react_type(i)%n_data
        if (react_type(i)%n_new(j).gt.0) &
             deallocate(react_type(i)%new(j)%prod_type)
     end do
     deallocate(react_type(i)%n_new)
     deallocate(react_type(i)%new)
     if (react_type(i)%n_react_group.gt.0) &
          deallocate(react_type(i)%react_group)
  end do
  if (n_react_type.gt.0) deallocate(react_type)
  if (n_react_type.gt.0) deallocate(react_map)

  ! geometric object
  do i=1,n_geom_obj
     if (geom_obj(i)%does_diss) then
        do j=1,geom_obj(i)%diss_type
           deallocate(geom_obj(i)%diss_obj(j)%diss_type)
        end do
        deallocate(geom_obj(i)%diss_eb)
        deallocate(geom_obj(i)%diss_nu)
        deallocate(geom_obj(i)%diss_rd)
        deallocate(geom_obj(i)%diss_const_rate)
        deallocate(geom_obj(i)%diss_n)
        deallocate(geom_obj(i)%diss_obj)
     end if
  end do
  if (n_geom_obj.gt.0) deallocate(geom_obj)

  ! Sink-react type
  do i=1,n_sink_type
     deallocate(sink_type(i)%react_eb)
     deallocate(sink_type(i)%react_nu)
     deallocate(sink_type(i)%react_rd)
     deallocate(sink_type(i)%react_rd2)
     deallocate(sink_type(i)%react_const_rate)
     do j=1,sink_type(i)%n_data
        if (sink_type(i)%n_new(j).gt.0) &
             deallocate(sink_type(i)%new(j)%prod_type)
     end do
     deallocate(sink_type(i)%n_new)
     deallocate(sink_type(i)%new)
  end do
  if (n_sink_type.gt.0) deallocate(sink_type)
  if (n_sink_type.gt.0) deallocate(sink_map)

  ! box parameters
  deallocate(vbox)
  deallocate(vbox_map)

  ! Profile mape
  deallocate(profile_map_diff)
  deallocate(profile_map_diss)
  if (n_react_type.gt.0) deallocate(profile_map_rctn)
  if (n_ext.gt.0) deallocate(profile_map_extn)
  if (n_sink_type.gt.0) deallocate(profile_map_sink)
  if (n_geom_obj.gt.0) deallocate(profile_map_srce)

  ! table param
  do i=1,total_alloc_sim_obj
     if (sim_obj(i)%n_alloc_event.gt.0) &
          deallocate(sim_obj(i)%event_table_list)
  end do
  deallocate(sim_obj)
  deallocate(del_sim_obj)
  deallocate(event_table)
  deallocate(del_event_table)
  deallocate(spont_event_table)
  do i=1,total_alloc_event_profile
     if (event_profile(i)%n_alloc_event.gt.0) &
          deallocate(event_profile(i)%event_list)
  end do
  deallocate(event_profile)

  ! Count objects
  do i=1,n_object_group
     deallocate(count_obj(i)%nx)
     deallocate(count_obj(i)%ny)
     deallocate(count_obj(i)%nz)
  end do
  deallocate(count_obj)
  deallocate(total_count_obj)

  if (print_spatial_dist) then
     deallocate(spatial_file_name%fnx)
     deallocate(spatial_file_name%fny)
     deallocate(spatial_file_name%fnz)
  end if

  return
  
end subroutine freemem
